﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using CWM.Client.Models;
using CWM.Client.Utils;

namespace CWM.Client.Data
{
    public partial class DynamicDataProvider
    {
        public bool Cache { get; set; }

        public bool Optional { get; set; }

        

        /// <summary>
        /// 角色
        /// </summary>
        public List<Role> Role
        {
            get
            {
                DataTable dt = null;
                if (Cache == true)
                {
                    dt = DataCache.Role;
                }
                if (dt == null)
                {
                    dt = DataCache.LoadRole();
                }
                Role[] array = new Role[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    array[i] = new Role()
                    {
                        Id = Converter.Convert<int>(dt.Rows[i]["ID"]),
                        Name = Converter.Convert<string>(dt.Rows[i]["NAME"]),
                        Description = Converter.Convert<string>(dt.Rows[i]["DESCRIPTION"]),
                        Permission = Converter.Convert<string>(dt.Rows[i]["PERMISSION"]),
                        CreateUser = Converter.Convert<string>(dt.Rows[i]["CREATE_USER"]),
                        CreateDate = Converter.Convert<DateTime>(dt.Rows[i]["CREATE_DATE"]),
                        ModifyUser = Converter.Convert<string>(dt.Rows[i]["MODIFY_USER"]),
                        ModifyDate = Converter.Convert<DateTime>(dt.Rows[i]["MODIFY_DATE"]),
                        Status = Converter.Convert<short>(dt.Rows[i]["STATUS"])
                    };
                }
                List<Role> list = array.ToList();
                if (Optional == true)
                {
                    list.Insert(0, new Role()
                    {
                        Name = Language.Get("Lang.None")
                    });
                }
                return list;
            }
        }


        //后台用户（管理员）
        public List<User> User
        {
            get
            {
                DataTable dt = null;
                if (Cache == true)
                {
                    dt = DataCache.User;
                }
                if (dt == null)
                {
                    dt = DataCache.LoadUser();
                }

                User[] array = new User[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    array[i] = new User()
                    {
                        Id = Converter.Convert<string>(dt.Rows[i]["ID"]),
                        Account = Converter.Convert<String>(dt.Rows[i]["ACCOUNT"]),
                        Name = Converter.Convert<String>(dt.Rows[i]["NAME"])
                    };
                }
                List<User> list = array.ToList();
                if (Optional == true)
                {
                    list.Insert(0, new User()
                    {
                        Name = Language.Get("Lang.None")
                    });
                }
                return list;
            }
        }
        //前台用户（客户）
        public List<Customer> Client
        {
            get
            {
                DataTable dt = null;
                if (Cache == true)
                {
                    dt = DataCache.Client;
                }
                if (dt == null)
                {
                    dt = DataCache.LoadClient();
                }

                Customer[] array = new Customer[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    array[i] = new Customer()
                    {
                        Id = Converter.Convert<int>(dt.Rows[i]["ID"]),
                        Account = Converter.Convert<String>(dt.Rows[i]["ACCOUNT"]),
                        Name = Converter.Convert<String>(dt.Rows[i]["NAME"]),
                        ManagerName = Converter.Convert<string>(dt.Rows[i]["MANAGER_NAME"]),
                        CreateDate = Converter.Convert<DateTime>(dt.Rows[i]["CREATE_DATE"])
                    };
                }
                List<Customer> list = array.ToList();
                if (Optional == true)
                {
                    list.Insert(0, new Customer()
                    {
                        Name = Language.Get("Lang.None")
                    });
                }
                return list;
            }
        }
    }
}